package com.tuniu.agents.util;

import com.tuniu.agents.common.AgentsConstants;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.util.StringUtils;

import java.time.LocalDate;

public class ConversationUtil {

    private static final String CONVERSATION_ID_KEY = "conversation_id";
    private static final String USER_LAT_KEY = "user_lat";
    private static final String USER_LNG_KEY = "user_lng";
    private static final String USER_CITY_CODE_KEY = "user_city_code";
    private static final String USER_CITY_NAME_KEY = "user_city_name";
    private static final String DEFAULT_CITY_NAME_KEY = "departureCityName";

    // Get the conversation ID from the ToolContext
    public static String getConversationId(ToolContext toolContext) {
        Object conversationIdObj = toolContext.getContext().get(CONVERSATION_ID_KEY);
        if (!(conversationIdObj instanceof String)) {
            throw new IllegalArgumentException("Invalid or missing conversation_id in context");
        }
        return (String) conversationIdObj;
    }

    // Get the user's latitude from the ToolContext
    public static Double getUserLat(ToolContext toolContext) {
        Object userLat = toolContext.getContext().get(USER_LAT_KEY);
        if (!(userLat instanceof Double)) {
            return null;
        }
        return (Double) userLat;
    }

    // Get the user's longitude from the ToolContext
    public static Double getUserLng(ToolContext toolContext) {
        Object userLng = toolContext.getContext().get(USER_LNG_KEY);
        if (!(userLng instanceof Double)) {
            return null;
        }
        return (Double) userLng;
    }

    // Get the user's city code from the ToolContext
    public static String getUserCityCode(ToolContext toolContext) {
        Object userCityCode = toolContext.getContext().get(USER_CITY_CODE_KEY);
        if (!(userCityCode instanceof String)) {
            return null;
        }
        return (String) userCityCode;
    }

    // Get the user's city name from the ToolContext
    public static String getUserCityName(ToolContext toolContext) {
        Object userCityName = toolContext.getContext().get(USER_CITY_NAME_KEY);
        if (!(userCityName instanceof String)) {
            return null;
        }
        return (String) userCityName;
    }

    // Get the default city name from the ToolContext
    public static String getDefaultCityName(ToolContext toolContext) {
        Object defaultCityName = toolContext.getContext().get(DEFAULT_CITY_NAME_KEY);
        if (!(defaultCityName instanceof String)) {
            return AgentsConstants.FrontendParamKey.DEFAULT_VALUE_CITY_KEY;
        }
        return (String) defaultCityName;
    }

    // Get the default check-in date
    public static String getDefaultCheckIn() {
       return LocalDate.now().toString();
    }

    // Get the default check-out date based on the check-in date
    public static String getDefaultCheckOut(String checkIn) {
        if (StringUtils.hasText(checkIn)){
            return LocalDate.parse(checkIn).plusDays(1).toString();
        } else {
            return LocalDate.parse(getDefaultCheckIn()).plusDays(1).toString();
        }
    }

}
